package cn.itcast.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SaasException  implements HandlerExceptionResolver{

    //统一异常处理器  一旦出现错误都会进入此方法
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        ModelAndView mv =new ModelAndView();

        if(ex instanceof UnauthorizedException){
            //跳转到unauthorized.jsp
            mv.setViewName("forward:/unauthorized.jsp"); //不通过视图解析器的
        }else{
            //错误信息跳转页面
            mv.setViewName("error");
        }

        //错误信息提示信息
        mv.addObject("errorMsg",ex.getMessage());
        return mv;
    }
}
